package com.eliza.springboot.v3.http.control.webFlux

import com.eliza.springboot.v3.repository.entity.Song
import com.eliza.springboot.v3.repository.repo.postgresql.SongRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController

@RequestMapping("/resp")
@RestController
class Resp {
    @Autowired
    private lateinit var songRepository: SongRepository

    @RequestMapping("/database")
    fun database(): ResponseEntity<Page<Song>> {
        return ResponseEntity.ok(songRepository.findAll(PageRequest.of(0, 10)))
    }

    @RequestMapping("/string")
    fun song(): String {
        return "Hello World"
    }

    @RequestMapping("/obj")
    fun json(): Song {
        return Song(1, "as")
    }

    @RequestMapping("/map")
    fun map(): Map<String, Any> {
        return mapOf("name" to "name", "age" to 1)
    }

    @RequestMapping("/array")
    fun array(): Array<String> {
        return arrayOf("a", "As")
    }

    /*
    * 1 只有这个类发生地错误
    * */


    @ResponseBody
    @ExceptionHandler(Exception::class)
    fun handleError(e: Exception): String {
        return "Error:${e.message}"
    }
}